home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Metrowerks / com / metrowerks / AppletFrame.java
Encoding:
Java Source  |  1999-05-28  |  5.0 KB  |  209 lines  |  [TEXT/CWIE]

  1. package com.metrowerks;
  2.  
  3. // Generic AppletFrame support.
  4. // Gives a (simplistic) framework that emulates (most of)
  5. // what a browser would provide to an applet.
  6. //
  7. // March 98 - J. Cortell, A. Gill
  8.  
  9. import java.util.*;
  10. import java.awt.*;
  11. import java.awt.event.*;
  12. import java.applet.*;
  13.  
  14. public class AppletFrame extends Frame
  15.     implements WindowListener
  16. {
  17.     private static final String extraTitle = " - Applet Window";
  18.  
  19.     // Support the old calling convention.
  20.     
  21.     public static void startApplet(
  22.                                 String className, 
  23.                                 String title,
  24.                                 String[] args) {
  25.     
  26.         Applet applet;
  27.  
  28.         try {
  29.             // create an instance of your applet class
  30.             applet = (Applet) Class.forName(className).newInstance();
  31.         } catch (ClassNotFoundException e) {
  32.             return;
  33.         } catch (InstantiationException e) {
  34.             return;
  35.         } catch (IllegalAccessException e) {
  36.             return;
  37.         }
  38.     
  39.         startApplet(applet,title,new Hashtable(),200,200);
  40.     }
  41.     
  42.     // This is the real entry point.
  43.     // First, we have the name of the applet class,
  44.     // Second, the title for the window,
  45.     // Next, the Hashtable that contains the
  46.     //     parameter -> parameter value mapping.
  47.     // This simulates the <param ...> tag.
  48.     // Finally the width and height of the applet.
  49.     
  50.     public static void startApplet(
  51.                                 Applet applet, 
  52.                                 String title,
  53.                                 Hashtable params,
  54.                                 int width,
  55.                                 int height) {
  56.  
  57.  
  58.         // setup so as getParameter, etc, will work
  59.         OurAppletContext newAppletContext 
  60.             = new OurAppletContext(applet.getToolkit());
  61.         OurAppletStub newAppletStub 
  62.             = new OurAppletStub(newAppletContext,params);
  63.         applet.setStub(newAppletStub);
  64.  
  65.         // create new application frame window
  66.         AppletFrame f = new AppletFrame(title + extraTitle);
  67.     
  68.         // add applet to frame window
  69.         f.add("Center", applet);
  70.  
  71.         // add a quit menu item
  72.         MenuBar menubar = new MenuBar();
  73.         Menu file = new Menu("File", true);
  74.         MenuItem item = new MenuItem("Quit");
  75.         menubar.add(file);
  76.         file.add(item);
  77.         f.setMenuBar(menubar);
  78.         item.addActionListener(new ActionListener() {
  79.             public void actionPerformed(ActionEvent e) {
  80.                 // At this point, we simply leave.
  81.                 java.lang.Runtime.getRuntime().exit(0);
  82.             } 
  83.         });
  84.  
  85.         
  86.         // resize frame window to fit applet
  87.         f.pack();
  88.         f.setSize(width,height);
  89.         applet.setSize(width,height);
  90.  
  91.         // initialize the applet
  92.         applet.init();
  93.         applet.start();
  94.  
  95.         // show the window
  96.         f.show();
  97.  
  98.         f.repaint();
  99.     
  100.     }  // end startApplet()
  101.  
  102.  
  103.     // constructor needed to pass window title to class Frame
  104.     public AppletFrame(String name) {
  105.         // call java.awt.Frame(String) constructor
  106.         super(name);
  107.         addWindowListener(this);
  108.     }
  109.  
  110.     public void windowActivated(WindowEvent e) {}
  111.     public void windowClosed(WindowEvent e) {}
  112.     public void windowClosing(WindowEvent e) {
  113.         // At this point, we simply leave.
  114.         java.lang.Runtime.getRuntime().exit(0);
  115.     }
  116.     public void windowDeactivated(WindowEvent e) {}
  117.     public void windowDeiconified(WindowEvent e) {}
  118.     public void windowIconified(WindowEvent e) {}
  119.     public void windowOpened(WindowEvent e) {}
  120. }   // end class AppletFrame
  121.  
  122. class OurAppletContext implements AppletContext {
  123.     private Toolkit ourToolkit;
  124.  
  125.     OurAppletContext(Toolkit toolkit) {
  126.         ourToolkit = toolkit;
  127.     }
  128.  
  129.     public Image getImage(java.net.URL url) {
  130.         return ourToolkit.getImage(url);
  131.     }
  132.  
  133.     // The rest are blank/void for this
  134.     // implementation of our applet viewer.
  135.     
  136.     public AudioClip getAudioClip(java.net.URL url) { 
  137.         // Think of silent movies...
  138.         return new OurAudioClip(url);
  139.     }
  140.  
  141.     public Applet getApplet(String name) { return null; }
  142.     public Enumeration getApplets() { return null; }
  143.     public void showDocument(java.net.URL url) {}
  144.     public void showDocument(java.net.URL url,String target) {}
  145.     public void showStatus(String status) {}
  146. }
  147.  
  148. class OurAppletStub implements AppletStub {
  149.     private Hashtable paramTable;
  150.     private AppletContext appletContext;
  151.     private java.net.URL codeBase;
  152.     private java.net.URL documentBase;
  153.  
  154.     OurAppletStub(AppletContext appContext,Hashtable params) {
  155.         appletContext = appContext;
  156.         paramTable = params;
  157.     }
  158.  
  159.     public boolean isActive() { return true; }
  160.  
  161.     public java.net.URL getDocumentBase() { 
  162.         if (documentBase == null) {
  163.             try {
  164.                 java.io.File file = new java.io.File("");
  165.                 documentBase = new java.net.URL("file",
  166.                         "",  // no host 
  167.                         file.getAbsolutePath().toString());
  168.             }
  169.             catch (java.net.MalformedURLException e) {
  170.             }
  171.         }
  172.         return documentBase;
  173.     }
  174.  
  175.     public java.net.URL getCodeBase() { 
  176.         if (codeBase == null) {
  177.             try {
  178.                 java.io.File file = new java.io.File("");
  179.                 codeBase = new java.net.URL("file",
  180.                         "",  // no host 
  181.                         file.getAbsolutePath().toString());
  182.             }
  183.             catch (java.net.MalformedURLException e) {
  184.             }
  185.         }
  186.         return codeBase;
  187.     }
  188.  
  189.     public String getParameter(String str) { 
  190.         return (String) paramTable.get(str.toUpperCase());
  191.     }
  192.  
  193.     public AppletContext getAppletContext() { 
  194.         return appletContext; 
  195.     }
  196.     public void appletResize(int x,int y) {
  197.         // since the browser would ignore this,
  198.         // we choose to ignore it as well.
  199.     }
  200. }
  201.  
  202. // Dummy sound class.
  203. class OurAudioClip implements AudioClip {
  204.     public OurAudioClip(java.net.URL url) {}
  205.     public void loop() {}
  206.     public void play() {}
  207.     public void stop() {}
  208. }
  209.